<?php
class ProjectVersion extends Zend_Db_Table
{
	protected $_name = "projectversion";
	public function getProjectVersions($projectId)
	{
		$select = $this->_db->select()
							->from($this->_name)
							->where('project = ?', $projectId);
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
	public function getProjectVersion($projectId)
	{
		$select = $this->_db->select()
							->from($this->_name)
							->where('project = ?', $projectId);
		//echo $select;
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
	public function getList($projectId)
	{
		$select = $this->_db->select()
							->from($this->_name,array('key'=>'id','value'=>'vname'))
							->where('project = ?', $projectId);
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	public function getData($pid)
	{
		$select = $this->_db->select()
							->from(array('pv'=>$this->_name))
							/* ->join(array('comp_issues'=>'component_issues'),
								'cp.id = comp_issues.component',"count('comp_issues.id')  as issues")
							->group('cp.id')*/
							->join(array('nodeAssoc'=>'nodeassociation'),
							 					'pv.id = nodeAssoc.sink_node_id', null)
							->join(array('issue'=>'issue'),'issue.id = nodeAssoc.source_node_id',"count(*) as v_issues")
							->group('pv.id')
							->where('nodeAssoc.association_type = "IssueFixVersion"')
							->where('pv.project = ?',$pid);
		//echo $select;
		$versions = $this->getAdapter()->fetchAll($select);
		return $versions;
	}
	
	public function getComponentVersionData($pid, $cid)
	{
		$select = $this->_db->select()
							->from(array('pv'=>$this->_name),array('vname'))
							->join(array('nodeAssoc'=>'nodeassociation'),
											'pv.id = nodeAssoc.sink_node_id', null)
							->join(array('issue'=>'issue'),
											'issue.id = nodeAssoc.source_node_id',
												"count(*) as v_issues")
							->group('pv.id')
							->where("nodeAssoc.association_type = 'IssueFixVersion' AND (nodeAssoc.source_node_id IN (select nodeassociation.source_node_id from nodeassociation where nodeassociation.sink_node_id = $cid AND nodeassociation.association_type = 'IssueComponent'))")
							->where('pv.project = ?',$pid);
							
		$versions = $this->getAdapter()->fetchAll($select);
		return $versions;
	}
	
	public function getIssueFixVersions($id)
	{
		$select = $this->_db->select()
							->from(array('pv'=>$this->_name),array('id','vname'))
							 ->join(array('nodeAssoc'=>'nodeassociation'),
							 					'pv.id = nodeAssoc.sink_node_id', null)
							->where('nodeAssoc.source_node_id = ?', $id)
							->where('association_type = "IssueFixVersion"');
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
	public function getIssueAffectVersions($id)
	{
		$select = $this->_db->select()
							->from(array('pv'=>$this->_name),array('id','vname'))
							 ->join(array('nodeAssoc'=>'nodeassociation'),
							 					'pv.id = nodeAssoc.sink_node_id', null)
							->where('nodeAssoc.source_node_id = ?', $id)
							->where('association_type = "IssueVersion"');
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
}